package org.tuskit.model
{
	import mx.collections.ArrayCollection;
	import lib.TuskitLib;
	import org.tuskit.model.TuskitModelLocator;
	import mx.controls.Alert;
		
	public class Project
	{
		public static const UNSAVED_ID:Number = -1; 
		public static const NONE_ID:Number = 0;
		
		public static const NONE:Project = 
			new Project (0 ,null,'','','',null,null,null as int,false,0.0,NONE_ID);
		
		[Bindable]
		public var id:int;
		
		[Bindable]
		public var parent:Project;
		
		[Bindable]
		public var parent_id:int;
		
		[Bindable]
		public var tracker:Tracker;
		
		[Bindable]
		public var name:String;
		
		[Bindable]
		public var description:String;
		
		[Bindable]
		public var trackerProject:String;
		
		[Bindable]
		public var startDate:Date;
		
		[Bindable]
		public var endDate:Date;
		
		[Bindable]
		public var iterationLength:int;
		
		[Bindable]
		public var progressReport:Boolean;
		
		[Bindable]
		public var workUnitsReal:Number;
		
		[Bindable]
		public var children:ArrayCollection;
		
		[Bindable]
		public var iterations:ArrayCollection;
		
		[Bindable]
		public var selectedIteration:Iteration;
		
		public function Project(
			parent_id:int = 0,
			tracker:Tracker = null,
			name:String = '',
			description:String = '',
			trackerProject:String = '',
			startDate:Date = null,
			endDate:Date = null,
			iterationLength:Number = 14,
			progressReport:Boolean = false,
			workUnitsReal:Number=0.0,
			id:Number = UNSAVED_ID
			):void {
			
			this.parent_id = parent_id;
		
			this.name = name;
			this.description = description;
			this.startDate = startDate;
			this.endDate = endDate;
			this.iterationLength = iterationLength;
			this.progressReport = progressReport;
			this.workUnitsReal = workUnitsReal;
			this.id = id;
			this.children = new ArrayCollection([]);
			this.iterations = new ArrayCollection([]);	
		}
		
		public function addChild(project:Project):void {
			project.parent = this;
			if (!this.children.contains(project)) {
            	this.children.addItem(project);
   			}
		}
		
		public function removeChild(project:Project):void {
            if (project.parent == this) {
	            for (var i:int = 0; i < this.children.length; i++) {
	                if (this.children[i].id == project.id) {
	                    this.children.removeItemAt(i);
	                    project.parent = Project.NONE;
	                    break;	
					}
				}
         	}
  		}
  		
  		public function addIteration(iteration:Iteration):void {
  			iteration.project = this;
  			for each (var i:Iteration in iterations.source) {
  				if (i.id == iteration.id) return;
  			}
  			this.iterations.addItem(iteration);
  		}
  		
  		public function removeIteration(iteration:Iteration):void {
  			if (iteration.project == this) {
  				for (var i:int = 0; i< this.iterations.length; i++) {
  					if (this.iterations[i].id == iteration.id) {
  						this.iterations.removeItemAt(i);
  						iteration.project = Project.NONE;
  						break;
  					}
  				}
  			}
  		}
  		
  				/**
		 * Uglyish method to update Tracker in rails.
		 * We cannot use xml with _method=PUT, so we have to assemble 
		 * something similar to rails form
		 */
		public function	toUpdateObject():Object {
			var obj:Object = new Object();
			obj["Project[parent_id]"] = TuskitLib.nullifyId(parent.id);
			obj["Project[tracker_id]"] = TuskitLib.nullifyId(tracker.id);
			obj["Project[name]"] = name;
			obj["Project[description]"] = description;
			obj["Project[trackerProject]"] = trackerProject;
			obj["Project[startDate]"] = startDate;
			obj["Project[endDate]"] = endDate;
			obj["Project[iterationLength]"] = iterationLength;
			obj["Project[progressReport]"] = progressReport;
			obj["Project[id]"] = id;
			return obj	
		}
			
			
		public function toXML():XML {
			var retval:XML = 
				<project>
					<parent_id>{parent_id}</parent_id>
					<tracker_id>{tracker.id}</tracker_id>
					<name>{name}</name>
					<description>{description}</description>
					<trackerProject>{trackerProject}</trackerProject>
					<startDate>{startDate}</startDate>
					<endDate>{endDate}</endDate>
					<iterationLength>{iterationLength}</iterationLength>
					<progressReport>{progressReport}</progressReport>
					<id>{id}</id>
				</project>;
			return retval;
		}

		public static function fromXML(project:XML):Project {
			var model:TuskitModelLocator = TuskitModelLocator.getInstance();
			var retval:Project;
			var parent_id:int = int(project.parent_id);
			var tracker_id:int = int(project.tracker_id);
			var startDate:Date = TuskitLib.fromXmlDate(project.startDate as XML);
			var endDate:Date = TuskitLib.fromXmlDate(project.endDate as XML);
			var progressReport:Boolean = TuskitLib.fromXmlBoolean(project.progress_reports);
			retval = new Project(
				parent_id,
				project.tracker = model.getTracker(tracker_id),
				project.name,
				project.description,
				project.tracker_project,
				startDate,
				endDate,
				project.iteration_length as int,
				progressReport,
				project.work_units_real,
				project.id
			);
			return retval;
		}
	}
}